define(function(require, exports, module) {
	var RemoteCache=require('resources/js/component/RemoteCache.js');
	require('resources/js/component/TreeDropDown.js');
	
	var url={
		carrierType:'/resservice/ztreeCarrierTypeList.jhtml',
		catalogTreeData:'/catalogType/treeJson.jhtml?id=1',
		orgTreeData:'/resservice/ztreeOrgList.jhtml',
	};
	
	var ResInfoBaseEditor=function(data,$div,config){
		this.resInfo=data;
		this.$div=$div;
		
		this.baseUrl=config.baseUrl;
		this.step=config.step;
		this.readOnly=config.readOnly===undefined?false:config.readOnly;
		
		this.resInfo.authType=this.resInfo.authType==undefined?'':this.resInfo.authType;
		this.resInfo.catalogName=this.resInfo.catalogName||'';
		this.resInfo.catalog=this.resInfo.catalog||'';
		this.resInfo.orgName=this.resInfo.orgName||'';
		this.resInfo.org=this.resInfo.org||'';
		this.resInfo.shareType=this.resInfo.shareType==undefined?1:this.resInfo.shareType;
		
		this.init();
	};
	
	ResInfoBaseEditor.remoteCache=new RemoteCache();
	
	ResInfoBaseEditor.prototype={
		init:function(){
			ResInfoBaseEditor.remoteCache.get('catalogTreeData',this.getUrl('catalogTreeData'));
			ResInfoBaseEditor.remoteCache.get('orgTreeData',this.getUrl('orgTreeData'));
			ResInfoBaseEditor.remoteCache.get('carrierType',this.getUrl('carrierType'));
			this.render();
			this.bind();
			this.sync();
			this.readOnlyRender();
		},
		getUrl:function(key){
			return this.baseUrl+url[key];
		},
		get:function(key){
			return ResInfoBaseEditor.remoteCache.get(key)
		},
		render:function(){
			this.carrierTypeTree=new TreeDropDown({
				treeData:this.get('carrierType'),
				$input:$('input[name=carrierType-Name]',this.$div),
				$inputValue:$('input[name=carrierType]',this.$div),
				readOnly:this.readOnly,
				desc:'请选择'	//空值描述的文字
			});
			
			this.step=='verify'&&this.renderVerify();
		},
		readOnlyRender:function(){
			if(!this.readOnly)
				return;
			
			$('input,textarea',this.$div).attr('readonly','readonly');
			$('select',this.$div).attr('disabled','disabled');
			$('[name=shareType]',this.$div).bootstrapToggle('disable');
		},
		renderVerify:function(){
			$('[name=shareType]',this.$div).bootstrapToggle({
		      	on: '是',
		      	off: '否',
		      	size:'mini',
		      	width:45,
		      	toggle:'toggle'
		    });
			this.catalogTree=new TreeDropDown({
				checkOnlyChild:false,
				readOnly:this.readOnly,
				treeData:this.get('catalogTreeData'),
				$input:$('input[name=catalogName]',this.$div),
				$inputValue:$('input[name=catalog]',this.$div),
				simpleData:false,
				expandAll:false,
				selectType:'checkbox',
				desc:'请选择'	//空值描述的文字
			});
			this.orgTree=new TreeDropDown({
				checkOnlyChild:false,
				treeData:this.get('orgTreeData'),
				$input:$('input[name=orgName]',this.$div),
				$inputValue:$('input[name=org]',this.$div),
				simpleData:true,
				expandAll:false,
				selectType:'checkbox',
				desc:'请选择'	//空值描述的文字
			});
		},
		bind:function(){
			var that=this;
			this.$shareType=$('input[name=shareType]',this.$div);
			this.$shareType.on('change',function(){
				that.resInfo.shareType=$(this).prop('checked')+1;
			});
		},
		sync:function(){
			var resInfo=this.resInfo
			this.carrierTypeTree.setValue(resInfo.carrierType);
			if(this.step=='verify')
			{
				if(this.resInfo.shareType==2)
					this.$shareType.bootstrapToggle('on').prop('checked',true);
				else
					this.$shareType.bootstrapToggle('off').prop('checked',false);
				//设值catalog, org的值
				resInfo.catalog=$.map(resInfo.resInfoCatalogs,function(e){return e.catalogId}).join(',');
				this.catalogTree.setValue(resInfo.catalog);
				resInfo.org=$.map(resInfo.resOrgAuthRels,function(e){return e.orgId}).join(',');
				this.orgTree.setValue(resInfo.org);
			}
		},
		fillData:function(resInfo){
			this.resInfo=resInfo;
			this.sync();
		},
		getError:function(){
			var res=[],resInfo=this.resInfo;
			!resInfo.bizName&&res.push('资源名称必须填写');
			!resInfo.carrierType&&res.push('信息载体必须填写');
			if(this.step=='verify')
			{
				!resInfo.resServicesCode&&res.push('资源编码必须填写');
				!resInfo.keyWord&&res.push('关键字必须填写');
				if(resInfo.catalog)
					resInfo.resInfoCatalogs=$.map(resInfo.catalog.split(','),function(catalogId){
						return {
							catalogId:catalogId,
							resId:resInfo.resId
						}
					});
				else	
					res.push('分类至少选择一个');
				!resInfo.authType&&res.push('共享类型必须选择');
				if(resInfo.authType=='12')
				{
					if(resInfo.org)
						resInfo.resOrgAuthRels=$.map(resInfo.org.split(','),function(orgId){
							return {
								orgId:orgId,
								resId:resInfo.resId
							}
						});
					else
						res.push('受限组织至少选择一个');
				}	
				
			}
			return res;
		}
	};
	
	return ResInfoBaseEditor;
	
});